using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    [Header("跟随目标")]
    public Transform target; // 要跟随的精灵

    [Header("平滑设置")]
    public float smoothSpeed = 0.125f; // 平滑插值速度
    public Vector3 offset = new Vector3(0, 0, -10); // 摄像机偏移（保持Z=-10以正确显示2D画面）

    [Header("边界限制（可选）")]
    public bool useLimits = false;
    public float minX, maxX, minY, maxY;

    private Vector3 velocity = Vector3.zero;

    void LateUpdate()
    {
        if (target == null) return;

        // 目标位置 + 偏移
        Vector3 desiredPosition = target.position + offset;

        // 限制坐标范围
        if (useLimits)
        {
            desiredPosition.x = Mathf.Clamp(desiredPosition.x, minX, maxX);
            desiredPosition.y = Mathf.Clamp(desiredPosition.y, minY, maxY);
        }

        // 平滑插值移动摄像机
        transform.position = Vector3.SmoothDamp(
            transform.position,
            desiredPosition,
            ref velocity,
            smoothSpeed
        );
    }
}